const express = require('express');
const router = express.Router();
module.exports = router;

// const app = express();

//加载配置文件
const config = require('../config/mysql.json');
const mysql = require('mysql');
const conn = mysql.createConnection(config);


router.get('/select',(req,res)=>{
    const id = req.body.id || req.query.id;
    const sqlStr = 'select * from user' +(id?'where id=?':"");
    console.log(sqlStr);

    conn.query(sqlStr, id, (err, result) => {
        if (err){
            // return result.json({code: 404, data: '获取失败'})
            console.log('获取失败');
            return  res.json({code: 404, data: '获取失败'});;
        }
        res.json({
            code: 0, data: result
        })
    });
})
// https://www.pianshen.com/article/4824305754/


/*
1.1	用户注册
接口URL
	{{url}}/user/register
请求方式
	POST
请求 Content-Type
	application/x-www-form-urlencoded
请求Body参数
	uname	zhangsan	必填	-用户名
	upwd	123456	必填	-密码
	phone	13333333333	必填	-手机号
成功响应示例
{
    "code": 200,
    "msg": "register success",
    "data": {
        "uid": 7,
        "uname": "zhangsan"
    }
}
*/
router.post('/register',(req,res,next)=>{
    //要输出给客户端的数据
    let output = {
        code:200,
        msg:'提交成功',
        data:null
    }
    // 获取传入的参数
    let formContent = req.body
    console.log(formContent)
    //获取用户名 密码 手机号
    const uname = formContent.name
    if(!uname){
        output.code = 401
        output.msg ='用户名为空'
        res.send(output)
        return
    }
    const upwd = formContent.upwd
    if(!upwd){
        output.code = 402
        output.msg ='密码为空'
        res.send(output)
        return
    }
    //
    let sex = formContent.sex
    if (!sex) {
        output.code = 403
        output.msg = 'sex required'
        res.send(output)
        return
    }
    let college = formContent.college
    if (!college) {
        output.code = 405
        output.msg = 'college required'
        res.send(output)
        return
    }
    let major = formContent.major
    if (!major) {
        output.code = 406
        output.msg = 'major required'
        res.send(output)
        return
    }
    let mail = formContent.mail
    if (!mail) {
        output.code = 407
        output.msg = 'mail required'
        res.send(output)
        return
    }
    let phone = formContent.phone
    if (!phone) {
        output.code = 408
        output.msg = 'phone required'
        res.send(output)
        return
    }

    // sql 查询语句
    let sql =
        'SELECT student_id FROM student WHERE student_name=? OR student_phone=? OR student_email=?'
    conn.query(sql,[uname, phone, mail],(err,result)=>{
        if(err){
            console.log(err);
            next(err)
        }

        if(result.length>0){
            //查询到同名或者同电话的用户
            output.code = 409
            output.msg = 'uname or phone or e-mail already taken'
            res.send(output)
        }else{
            console.log('执行查询数据成功！')
            //未查询到同名或同电话的用户
            //执行数据库插入操作 —— 等待验证完用户名不存在，再执行插入操作
            let sql =
                'INSERT INTO student(student_password,student_name,student_academy,student_major,student_sex,student_email,student_phone) VALUES(md5(?),?,?,?,?,?,?)'
            conn.query(
                sql,
                [upwd, uname, college, major, sex, mail, phone],
                (err, result) => {
                    // console.log(res.json(result))
                    if (err) next(err) //此处不允许throw err！！只能把错误交给后续的Express错误处理中间件处理
                    output.code = 200
                    output.msg = 'register success'
                    output.data = {
                        //获取数据库产生的自增用户编号
                        uid: result.insertId,
                        // uid: 1,
                        uname: uname
                    }
                    res.send(output)
                })
        }

    })
    // return res.send(output)

})


// https://juejin.cn/post/6844904198551666701#heading-17